# Copyright (C) 2006 Brian Davis

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""
Initilizes the Branches SCM abstraction library
"""

import os
import errors

def initialize_branch(path):
    """
    Given the current working branch's path, this function tries to import each 
    backend until the correct one is found. If it cannot find a suitable 
    candidate, it returns False. 
    """
    
    success = False
    
    cwd = os.getcwd()
    
    for module in os.listdir(__path__[0]):
        if module[0:4] == 'scm_':
            try:
                # This will try to import the appropriate module as 'branch'
                os.chdir(path)
                
                module = module.split(".py")[0]
                statement = 'import %s as branch' % (module,)                
                print statement                
                exec(statement)                
               	os.chdir(cwd)                
                return module
    			
            except errors.ModuleNotFoundError, e:
                print "Couldn't load %s" % (e.name,)
                return False

def dummy_function():
    """
    This is not being used, but contains some information that could be helpful
    later...
    """
    #    Creates a list of the currently available backend modules. This checks to 
    #    see if the client is in the user's path, as well. Thus, even if a backend
    #    module is installed, it will not be appended to this list unless the SCM
    #    client executable is in the path. 
        
    __scm_modules__ = []

    for module in os.listdir(__path__[0]):
        if os.path.isdir(os.path.join(__path__[0], module)):
            for path in os.environ['PATH'].split(os.pathsep):
                if os.path.exists(os.path.join(path, module)):
                    __scm_modules__.append(module)
                
    SCM_LIST = {                    \
                'bzr':'Bazaar-ng',  \
                'cdv':'Codeville',  \
                'cvs':'CVS',        \
                'darcs':'Darcs',    \
                'git':'Git',        \
                'hg':'Mercurial',   \
                'mtn':'Monotone',   \
                'svk':'SVK',        \
                'svn':'Subversion', \
                'tla':'Arch'        \
                }

    for path in os.environ['PATH'].split(os.pathsep):
        for scm in SCM_LIST:
            if os.path.exists(os.path.join(path, scm)):
                SCMS_PRESENT.append(scm)

    for scm in SCMS_PRESENT:
        print SCM_LIST[scm] + " found."
